﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.Practices.CompositeWeb;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public class VehicleModelEditPresenter : Presenter<IVehicleModelEditView>
    {
        public override void OnViewInitialized()
        {
            var service = ServiceContainer.GetService<IVehicleModelService>();
            var entity = service.GetVechicleModelById(View.VehicleModelID);
            View.PopulateVehicleModelData(entity);
        }

        public override void OnViewLoaded()
        {
            View.SaveVehicleModel+=new EventHandler(View_SaveVehicleModel);
        }

        void View_SaveVehicleModel(object sender, EventArgs e)
        {

            var service = ServiceContainer.GetService<IVehicleModelService>();
            var data = new VehicleModelData { Id = View.VehicleModelID };
            View.ExtractVehicleModelData(data);
            bool result;
            if (View.VehicleModelID == Guid.Empty)
            {
                result = service.AddVechicleModel(data);
            }
            else
            {
                result = service.ModifyVechicleModel(data);
            }
            if (result)
            {
                View.CloseWindow(true);
            }

        }
    }
}
